﻿using DevExpress.XtraEditors;
using eFatura.ceMessenger;
using Microsoft.Win32;
using Pechkin;
using System;
using System.Diagnostics;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;

namespace eFatura
{
    public partial class FaturaViewer : XPopupForm
    {
        public FaturaViewer(string url)
        {
            InitializeComponent();
            browser.Url = new Uri(url);
        }

        public string MasrafMerkezi = "";
        public FaturaViewer(string url, string MasrafMerkezi)
        {
            InitializeComponent();
            browser.Url = new Uri(url);
            this.MasrafMerkezi = MasrafMerkezi;
        }

        private void FaturaViewer_Load(object sender, EventArgs e)
        {
            MainToolbar.Visible = true;
        }

        private void btnYazdir_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Print();
        }

        private void Print()
        {
            String _footer = "";
            String _header = "";
            String key = @"Software\Microsoft\Internet Explorer\PageSetup";

            try
            {

                // read current user settings for header and footer

                _footer = Registry.CurrentUser.OpenSubKey(key).GetValue("footer").ToString();
                _header = Registry.CurrentUser.OpenSubKey(key).GetValue("header").ToString();

                // Replace current user settings for header and footer

                Registry.CurrentUser.OpenSubKey(key, true).SetValue("footer", "");
                Registry.CurrentUser.OpenSubKey(key, true).SetValue("header", "");
            }
            catch
            {
                // Sorun yok! Sayfa altında, IE ye ait URL bilgisi olacak.
            }

            browser.ShowPrintPreviewDialog();

            try
            {
                if (_footer != "")
                    Registry.CurrentUser.OpenSubKey(key, true).SetValue("footer", _footer);

                if (_header != "")
                    Registry.CurrentUser.OpenSubKey(key, true).SetValue("header", _header);
            }
            catch
            {
                // Sorun yok! Sayfa altında, IE ye ait URL bilgisi olacak.
            }

            browser.ShowPrintDialog();
        }

        private void SaveAsPDF()
        {
            byte[] buf = GetFaturaPDF();

            SaveFileDialog dlg = new SaveFileDialog();

            string url = browser.Url.PathAndQuery;
            int noStart = url.IndexOf("InvoiceNumber=");
            string invoiceNo = url.Substring(noStart).Replace("InvoiceNumber=", "");

            string dosyaAdi = MasrafMerkezi == "" ? invoiceNo + ".pdf" : MasrafMerkezi + ".pdf";

            dlg.Title = "Kaydet";
            dlg.FileName = dosyaAdi;
            dlg.Filter = "*.PDF|PDF";
            dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                FileStream fs = new FileStream(dlg.FileName, FileMode.Create);
                fs.Write(buf, 0, buf.Length);
                fs.Close();

                Process myProcess = new Process();
                myProcess.StartInfo.FileName = dlg.FileName;
                myProcess.Start();
            }
        }



        private byte[] GetFaturaPDF()
        {
            SimplePechkin sc = new SimplePechkin(new GlobalConfig().SetMargins(new Margins(150, 150, 50, 50))
               .SetDocumentTitle("e-Fatura")
               .SetCopyCount(1)
               .SetImageQuality(100)
               .SetLosslessCompression(true)
               .SetMaxImageDpi(-1)
               .SetOutlineGeneration(true)
               .SetOutputDpi(1200)
               .SetPaperOrientation(true)
               .SetPaperSize(PaperKind.Letter)
               );

            byte[] buf = sc.Convert(new ObjectConfig(), browser.DocumentText);
            return buf;
        }

        private void btnPDF_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SaveAsPDF();
        }

        private void btnMail_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ceMessengerSoapClient messenger = new ceMessengerSoapClient();

            string subject = "e-fatura";
            string message = "e-fatura";
            string url = browser.Url.PathAndQuery;
            int noStart = url.IndexOf("InvoiceNumber=");
            string invoiceNo = url.Substring(noStart).Replace("InvoiceNumber=", "");

            string dosyaAdi = MasrafMerkezi == "" ? invoiceNo + ".pdf" : MasrafMerkezi + ".pdf";
            string displayName = "Karbel e-fatura";
            string to = "kasimgulcan.karbel@gmail.com";

            messenger.SendEmailWithAttachmentFileName("ce", "---", displayName, to, "", subject, message, GetFaturaPDF(), dosyaAdi);
            XtraMessageBox.Show("e-Posta başarıyla gönderildi.", "Bilgi", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}